/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: MiscUtil.java
 *   @Date: 11/29/18 3:43 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package com.bhex.kline.utils;

/**
 * Created by ppzhao on 16/3/16.
 */

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;


public class MiscUtil {

    public static String getSimpleTimeStringHM(long time) {
        String strTemp = "";
        Date dateCreate = new Date(time);
        SimpleDateFormat myFmt2 = new SimpleDateFormat("HH:mm");
        myFmt2.setTimeZone(TimeZone.getDefault());
        strTemp = myFmt2.format(dateCreate);
        return strTemp;
    }

    public static String getSimpleTimeFormat(long time,String formatStr) {
        String strTemp = "";
        Date dateCreate = new Date(time*1000L);
        SimpleDateFormat myFmt2 = new SimpleDateFormat(formatStr);
        myFmt2.setTimeZone(TimeZone.getDefault());
        //myFmt2.setTimeZone(TimeZone.getTimeZone("UTC"));
        strTemp = myFmt2.format(dateCreate);
        return strTemp;
    }



    public static String getSimpleTimeStringHM(String timeString) {
        String strTemp = "";
        strTemp = timeString.substring(timeString.length()-4,timeString.length());
        strTemp = strTemp.substring(0,2)+":"+strTemp.substring(2,4);
        return strTemp;
    }

    public static String getSimpleTimeStringDay(String timeString) {
        String strTemp = "";
        strTemp = timeString.substring(0,4)+"-";
        strTemp += timeString.substring(4,6)+"-";
        strTemp += timeString.substring(6,8);
        return strTemp;
    }

    public static String getSimpleTimeFormat(String timeString,String formatStr1,String formatStr2){
        SimpleDateFormat format = new SimpleDateFormat(formatStr1);
        Date date = null;
        try {
            date = format.parse(timeString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        format = new SimpleDateFormat(formatStr2);
        String str = format.format(date);
        return str;
    }

	static public String roundFormat2(float num, int len) {
		String temp = "";
		if (num < 10000) {
			temp = roundFormat(num, len) + "";
		} else if (num >= 10000 && num < 100000000) {
			temp = roundFormat(num / 10000, len) + "万";
		} else {
			temp = roundFormat(num / 100000000, len) + "亿";
		}
		return temp;
	}
	
    static public String roundFormat(float num, int len) {
        String strTemp = "";

        try {
            BigDecimal b = new BigDecimal(String.valueOf(num));
            BigDecimal f1 = b.setScale(len, BigDecimal.ROUND_DOWN);
            strTemp = f1.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            strTemp = "";
        }
        return strTemp;
    }


    static public float roundFormatFloat(float num, int len) {
        float result = 0.0f;

        try {
            BigDecimal b = new BigDecimal(String.valueOf(num));
            BigDecimal f1 = b.setScale(len, BigDecimal.ROUND_DOWN);
            result = f1.floatValue();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    static public String roundFormat(double num, int len,int roundMode) {
        String strTemp = "";

        try {

            BigDecimal b = new BigDecimal(String.valueOf(num));
            BigDecimal f1 = b.setScale(len, roundMode);
            strTemp = f1.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            strTemp = "";
        }

        return strTemp;
    }

    static public int roundFormatFloat(float num, int len,int roundMode) {
        int strTemp = 0;

        try {

            BigDecimal b = new BigDecimal(String.valueOf(num));
            BigDecimal f1 = b.setScale(len, roundMode);
            strTemp = f1.intValue();
        } catch (Exception ex) {
            ex.printStackTrace();
            strTemp = 0;
        }

        return strTemp;
    }

    public static String mapToString(String url, Map<String, String> map) {
        StringBuffer sb = new StringBuffer();
        sb.append(url).append("?");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }
}

